package com.github.shiqiyue.app.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.shiqiyue.app.common.enums.EnableEnum;
import com.github.shiqiyue.app.modules.sys.entity.Permission;
import com.github.shiqiyue.app.modules.sys.mapper.PermissionMapper;
import com.github.shiqiyue.app.modules.sys.service.IPermissionService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author wenyao.wu
 * @since 2018-10-15
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {


    @Override
    public Permission findByPid(Long pid) {
        return this.getOne(new QueryWrapper<Permission>().eq(Permission.PID, pid));
    }

    @Override
    public Boolean isCodeValid(String code) {
        boolean isCodeValid = this.count(new QueryWrapper<Permission>()
                .eq(Permission.CODE, code)
                .eq(Permission.ENABLE, EnableEnum.ABLE)) == 0;
        return isCodeValid;
    }

    @Override
    public Boolean isCodeValid(String code, Long id) {
        boolean isCodeValid = this.count(new QueryWrapper<Permission>()
                .eq(Permission.CODE, code)
                .ne(Permission.ID, id)
                .eq(Permission.ENABLE, EnableEnum.ABLE)) == 0;
        return isCodeValid;
    }

    @Override
    public Boolean deleteById(Long id, Boolean soft) {
        if (soft) {
            Permission updateParam = new Permission();
            updateParam.setId(id);
            updateParam.setEnable(EnableEnum.DISABLE);
            return this.updateById(updateParam);
        }
        return this.removeById(id);
    }


}
